﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Your_Cart : System.Web.UI.Page
{
    #region Variables

    Shopping_Cart shopping;

    Boolean CheckTB;

    double val;
    double valpv;
    double valbv;

    #endregion

    #region DB Connection

    WebDBDataContext db = new WebDBDataContext();

    Tbl_UserPurchase u_purchase = new Tbl_UserPurchase();
   

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        #region Check Session

        if (Session["user"] != null)
        {
            loginuser.Text = Session["user"].ToString();
        }
        else
        {
            loginuser.Text = "Please login to continue your shopping  !!";
        }

        #endregion

        reBind();
    }


    #region Check dsatabase status 

    public bool Databse_check(string id) 
    {
        Bonus bonesch = new Bonus();
        try
        {
            bonesch = db.Bonus.Single(usr => usr.User_id.Equals(id));
            CheckTB = true;
        }
        catch 
        {
            CheckTB = false;
        }

        return CheckTB;
    }

    #endregion

    #region Emply check
    //public double Empty_Price(string id) 
    //{
    //    //have to check if there is any value-----

    //    Bonus chkbns = new Bonus();

    //    chkbns = db.Bonus.Single(usr => usr.User_id.Equals(id));
    //    string totdb = chkbns.T_sales.ToString();
    //    string totpv=chkbns.Tpv.ToString();
    //    string totbv=chkbns.Bv.ToString();
    //    if (totdb.Equals(""))
    //    {
    //        val = 0.00;
    //        valpv = 0.00;
    //        valbv = 0.00;
    //    }
    //    else
    //    {
    //        double dbtot_val = double.Parse(totdb);
    //        double dbpv_val=double.Parse(totpv);
    //        double dbbv_val = double.Parse(totbv);
    //        val = dbtot_val;
    //        valpv = dbpv_val;
    //        valbv = dbbv_val;
    //    }
    //    return val;

    //}
    #endregion

    #region Bind Values To Data Grid View



    public void reBind()
    {
        if (Session["cart"] != null)
        {
            shopping = (Shopping_Cart)Session["cart"];
            GridView1.DataSource = shopping.Items;
            GridView1.DataBind();
            Label1.Visible = false;
            lbl_pointval.Visible = false;
            lbl_businessval.Visible = false;
            if (shopping.Items.Count > 0)
            {
                Label1.Visible = true;
                lbl_pointval.Visible = true;
                lbl_businessval.Visible = true;
                Label1.Text = "" + shopping.total;
                lbl_pointval.Text = "" + shopping.totalPoint_value;
                lbl_businessval.Text = "" + shopping.totalBusiness_Value;
            }
        }
    }

    #endregion

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        #region Edit rows

        if (Session["cart"] != null)
        {

            GridView1.EditIndex = e.NewEditIndex;
            // Rebind the GridView control to show data in view mode.
            reBind();

        }
        #endregion
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        if (Session["cart"] != null)
        {

            Shopping_Cart cart = (Shopping_Cart)Session["cart"];

            string s = cart.Items[0].Produt_ID.ToString();
            string s1 = cart.Items[0].Product_Name.ToString();
            string s2 = cart.Items[0].Product_Code.ToString();
            float s3 = cart.Items[0].Point_Value;
            float s5 = cart.Items[0].Business_value;
            double s4 = cart.Items[0].Product_Price;

            //Response.Write(@"<script language='javascript'>alert('No error:.');</script>");



            TextBox Quantity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

            cart.updateItems(s, s2, s1, s4, s3, s5, int.Parse(Quantity.Text));



        }



        // Exit edit mode.
        GridView1.EditIndex = -1;

        // Rebind the GridView control to show data after updatin

        reBind();

        //Label1.Text = "Update process on going";
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        #region Cancel Edit Data grid Row

        GridView1.EditIndex = -1;
        reBind();

        #endregion
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        #region Delete Data Grid Row

        shopping.Items.RemoveAt(e.RowIndex);
        reBind();

        #endregion
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        #region Update Data Grid view

        if (e.CommandName == "UpdateQ")
        {
            if (Session["cart"] != null)
            {

                Shopping_Cart cart = (Shopping_Cart)Session["cart"];

                string s = cart.Items[0].Produt_ID.ToString();
                string s1 = cart.Items[0].Product_Name.ToString();
                string s2 = cart.Items[0].Product_Code.ToString();
                float s3 = cart.Items[0].Point_Value;
                float s5 = cart.Items[0].Business_value;
                double s4 = cart.Items[0].Product_Price;

                //Response.Write(@"<script language='javascript'>alert('No error:.');</script>");



                //  TextBox Quantity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

                // cart.updateItems(s, s2, s1, s4, s3, s5, int.Parse(Quantity.Text));   
            }

            // Exit edit mode.
            GridView1.EditIndex = -1;
            // Rebind the GridView control to show data after updatin
            reBind();
        }
        #endregion
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {

            if (Session["user"] != null)//check session User Login  
            {
                if (Session["cart"] != null)// check session shopping cart
                {

                    #region Get userID Refering Table

                    Tbl_User tb_user = new Tbl_User();

                    string username = Session["user"].ToString();
                    //get values userid using user table

                    tb_user = db.Tbl_Users.Single(usr => usr.Use_Name == username);

                    string uid = tb_user.Use_ID;//Using session Find uid 

                    #endregion

                    #region variables

                    string Prod_ID;
                    string Prod_CODE;
                    string Prod_NAME;
                    int Quan;
                    double Price;
                    float Point_VALUE;
                    float Buz_VALUE;

                    #endregion

                    #region get Grid view values and Send values to UserPurchase (Section OK)

                    foreach (GridViewRow GVRow in GridView1.Rows)
                    {


                        Prod_ID = GVRow.Cells[0].Text;
                        Prod_CODE = GVRow.Cells[1].Text;
                        Prod_NAME = GVRow.Cells[2].Text;
                        TextBox Quantity = (TextBox)GVRow.FindControl("TextBox1");
                        Quan = int.Parse(Quantity.Text);
                        Price = double.Parse(GVRow.Cells[4].Text);
                        Point_VALUE = float.Parse(GVRow.Cells[5].Text);
                        Buz_VALUE = float.Parse(GVRow.Cells[6].Text);


                        u_purchase.Use_ID = uid;//Refer user table and get values (user ID ok) 
                        u_purchase.Pro_ID = Prod_ID;
                        u_purchase.Code = Prod_CODE;
                        u_purchase.Qty = Quan;
                        u_purchase.Price = (decimal)Price;
                        u_purchase.PV = (decimal)Point_VALUE;
                        u_purchase.BV = (decimal)Buz_VALUE;

                        db.Tbl_UserPurchases.InsertOnSubmit(u_purchase);
                        db.SubmitChanges();




                    }
                    #endregion

                    #region Send values To Bonus Table


                    double Tot_val = double.Parse(Label1.Text);//shopping cart values
                    double Tot_pv = double.Parse(lbl_pointval.Text);
                    double Tot_bv = double.Parse(lbl_businessval.Text);

                    //have to check if ther is any values accoding to uid -----------------------------------------------------------------------------------
                    bool checkSt = Databse_check(uid);

                    if (!checkSt)
                    {

                        #region Insert values

                        Bonus bns_in = new Bonus();

                        bns_in.User_id = uid;
                        bns_in.T_sales = (decimal)(Tot_val);
                        bns_in.Tpv = (decimal)(Tot_bv);
                        bns_in.ppv = (decimal)(Tot_bv);
                        bns_in.Bv = (decimal)(Tot_pv);

                        db.Bonus.InsertOnSubmit(bns_in);
                        db.SubmitChanges();


                        #endregion

                    }
                    else
                    {

                        #region update


                        Bonus bns_up = new Bonus();

                        bns_up = db.Bonus.Single(usr => usr.User_id.Equals(uid));//uid using User table

                        double tbl_TP = double.Parse(bns_up.T_sales.ToString());
                        double tbl_PV = double.Parse(bns_up.Tpv.ToString());
                        double tbl_BV = double.Parse(bns_up.Bv.ToString());



                        bns_up.T_sales = (decimal)(tbl_TP + Tot_val);
                        bns_up.Tpv = (decimal)(tbl_PV + Tot_bv);
                        bns_up.ppv = (decimal)(tbl_PV + Tot_bv);//Every Month PPV --> 0
                        bns_up.Bv = (decimal)(tbl_BV + Tot_pv);

                        db.SubmitChanges();

                        #endregion

                    }
                    #endregion

                }

            }
            #region Back To login page
            else
            {
                Response.Redirect("login.aspx");//if not Loging Rederect to Login page
            }
            #endregion
        }
        catch (Exception) { Response.Redirect("404.aspx"); }
    }
}